<template>
  <div class='paper-dialog'>
    <el-dialog title="试卷编辑" class="m_dialog" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form ref="form" :model="formData" :rules="rules" label-width="160px" size="small">
        <el-form-item label="任务编号:" prop="taskNum">
          <el-input v-model="formData.taskNum" />
        </el-form-item>
        <el-form-item label="任务名称:" prop="taskName">
          <el-input v-model="formData.taskName" />
        </el-form-item>
        <el-form-item label="任务路线:" prop="taskLine">
          <el-input v-model="formData.taskLine" />
        </el-form-item>
        <el-form-item label="任务内容:" prop="taskRequire">
          <el-input v-model="formData.taskRequire" />
        </el-form-item>
        <el-form-item label="任务内容:" prop="taskContent">
          <el-input type="textarea" :rows="3" placeholder="请输入备注" v-model="formData.taskContent" />
        </el-form-item>
        <el-form-item label="任务状态:" prop="status">
          <el-select v-model="formData.status">
            <el-option v-for="dict in dict.type.inspection_project_type" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="btn_cancel">取 消</el-button>
        <el-button @click="submitForm" type="primary" class="btn_primary">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateOsiProject, addOsiProject } from '../api/project'

export default {
  dicts: ['inspection_project_type'],
  data () {
    return {
      dialogVisible: false,
      // 考试资料数据
      formData: {},
      // 试卷表单验证规则
      rules: {
        //  taskContent: undefined,
        // taskLine: undefined,
        // taskName: undefined,
        // taskNum: undefined,
        // taskRequire: undefined,
        taskContent: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        taskLine: [{ required: true, message: '请输入任务线路', trigger: 'blur' },],
        taskName: [{ required: true, message: '请输入任务名称', trigger: 'blur' },],
        taskNum: [{ required: true, message: '请输入编号', trigger: 'blur' }],
        taskRequire: [{ required: true, message: '请输入要求', trigger: 'blur' },],
      }
    };
  },
  computed: {},
  watch: {},
  created () {

  },
  mounted () {

  },
  methods: {
    show (data) {
      this.dialogVisible = true
      this.resetForm()

      if (data !== undefined) {
        this.$nextTick(() => {
          this.formData = JSON.parse(JSON.stringify(data))
        })
      }
    },
    // 清空formData 
    resetForm () {
      this.formData = {
        pageNum: 1, // 当前页
        pageSize: 10,  // 默认条数 
        taskContent: undefined,
        taskLine: undefined,
        taskName: undefined,
        taskNum: undefined,
        taskRequire: undefined,
        status: '0'
      }
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    // 提交Form表单
    async submitForm () {
      try {
        const isAddOperation = this.formData.inspectionProjectId === undefined
        const method = isAddOperation ? addOsiProject : updateOsiProject
        const message = isAddOperation ? '新增成功' : '修改成功'
        const response = await method(this.formData)
        if (response.code === 200) {
          this.dialogVisible = false
          this.$message.success(message)
          this.$emit('replace')
        }
      }
      catch (error) {
        this.$message.error('操作失败')
      }
    }
  }
}
</script>

<style lang='less' scoped>
</style>